#!coding=utf-8
import os
import socket
from flask import Flask
from flask import jsonify
from flask import request
import json

from flask_cors import CORS, cross_origin

print(socket.gethostname())

'''
API
	登录
	注册
	获取用户信息
'''

app = Flask("app")
# 跨域问题
cors = CORS(app)

'''
# 用户基本信息
{
	"account":"admin", // 登录账号
	"password" :"admin", // 登录密码
	"header":"null", // 头像地址
	"nick":"管理员", // 昵称
	"desc":"管理员不需要自我介绍", // 自我描述
	"birthday":"0000-00-00 00:00:00.000", // 生日 客户端 到日 即可
	"permission": -1, // 权限 , 数值越小, 能力越大
	"sex":0, // 性别: 0 没有设置; 1 男; 2 女
	"phone":"0576-90090000" // 电话 可以用来登录
}
// 请求体
{
	"time":"请求时间戳",
	"data":{}
}
// 响应体
{
	"msg":"",
	"code":0,
	"data":{}
}
'''

# 用户数据
user_list = [
	{
		"account":"admin",
		"password" :"admin",
		"header":"null",
		"nick":"管理员",
		"desc":"管理员不需要自我介绍",
		"birthday":"0000-00-00 00:00:00.000",
		"permission": -1,
		"sex":0,
		"phone":"0576-90090000"
	}
]

# 朋友关系 A -> B   === B -> A

# 申请添加 A -> B 
# A 发起申请, B 查看通过后删除记录

# 聊天记录

# 

@app.route("/", methods=['GET', 'POST'])
def get_home():
	# 获取参数 -> Get
	# request.args.get('key')
	# request.values.get('key')
	# 获取参数 -> Post
	# application/json:
	# request.get_json()['content'] / request.json.get('centent')
	# request.get_data() 原始数据
	# multipart/form-data:
	# request.form.get('key') / request.form['key']
	# application/x-www-form-urlencoded:
	# request.values.get('key')
    return "Hello"
	
@app.route("/allUser", methods=['POST'])
def all_user():
	return jsonify({
		"msg":"获取成功",
		"code":200,
		"data":user_list
	})


def check_user(account, phone):
	for u in user_list:
		if (u["account"] == account):
			return 4001
		if (u["phone"] == phone):
			return 4002
	return 200


@app.route("/registerCheck", methods=['POST'])
def check_register():
	# 获取输入手机号 登录所用用户名 检验是否可以进行注册
	# -> 接口调用时机 在用户输入框失去焦点后调用
	params = request.get_data()
	params = json.loads(params)
	print(params["account"])
	print(params["phone"])
	for u in user_list:
		if (u["account"] == params["account"]):
			return jsonify({
				"msg":"登录名称已占用",
				"code":4001,
				"data":None
			})
		if (u["phone"] == params["phone"]):
			return jsonify({
				"msg":"手机号已占用",
				"code":4002,
				"data":None
			})
	return jsonify({
				"msg":"登录名称和手机号通过测试",
				"code":200,
				"data":None
			})

@app.route("/register", methods=['POST'])
def register():
	# 获取注册手机号 登录用户名密码 检验是否可以进行注册 -> 接口调用时机 在用户输入
	params = request.get_data()
	params = json.loads(params)
	# 再次 check
	errCode = check_user(params["account"], params["phone"])
	if (errCode != 200):
		return jsonify({
				"msg":"注册失败",
				"code":errCode,
				"data":None
			})
	user = {
		"account":params["account"],
		"password" :params["password"],
		"header":params["header"],
		"nick":params["nick"],
		"desc":params["desc"],
		"birthday":params["birthday"],
		"permission":params["permission"],
		"sex":params["sex"],
		"phone":params["phone"]
	}
	# 添加用户到 list
	beList = len(user_list)
	user_list.append(user)
	afList = len(user_list)
	# 判断添加结果
	if (afList == beList + 1):
		return jsonify({
				"msg":"用户注册成功",
				"code":200,
				"data":None
			})
	else:
		return jsonify({
				"msg":"用户注册失败",
				"code":5001,
				"data":None
			})

@app.route("/login", methods=['POST'])
def login():
	params = request.get_data()
	params = json.loads(params)
	print(params["account"])
	print(params["password"])
	for u in user_list:
		if (u["account"] == params["account"]):
			if (u["password"] == params["password"]):
				return jsonify({
					"msg":"登录成功",
					"code":200,
					"data":u
				})
			else:
				return jsonify({
					"msg":"用户密码错误",
					"code":5001,
		"data":None
				})
	return jsonify({
		"msg":"用户不存在",
		"code":5002,
		"data":None
	})

@app.route("/user", methods=['POST'])
def get_user():
	params = request.get_data()
	params = json.loads(params)
	print(params["account"])
	for u in user_list:
		if (u["account"] == params["account"]):
			return jsonify({
				"msg":"获取用户信息成功",
				"code":200,
				"data":u
			})
	return jsonify({
		"msg":"请求用户信息失败",
		"code":5002,
		"data":None
	})


app.run(host='0.0.0.0', port=5001)